import BaseSprite from "../../Base/BaseSprite";
import CustomButton from "../../Common/CustomButton";
import { EResType } from "../../Common/Enum";
import { IStoreItem } from "../../Interface/IMessage";
import { LoadUtil } from "../../Util/LoadUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StoreItem extends BaseSprite {

    @property({ type: cc.Sprite, tooltip: "icon" })
    private icon: cc.Sprite = null;
    @property({ type: cc.Node, tooltip: "数量底图" })
    private box: cc.Node = null;
    @property({ type: cc.Label, tooltip: "数量" })
    private labNum: cc.Label = null;

    private data: IStoreItem = null;
    private callback: Function = null;

    protected onLoad() {
        super.onLoad();
    }

    public setData(type: 'hall' | 'up' | 'down' | 'game', data: IStoreItem, isMove: boolean = false, callback?: Function) {
        this.data = data;
        this.callback = callback;

        // 不挂按钮，会跟滑动事件冲突
        switch(type) {
            case 'hall':
                {
                    const comp = this.node.getComponent(CustomButton);
                    if (comp) {
                        this.node.removeComponent(comp);
                    }
                    this.box.active = false;
                    this.node.setPosition(cc.v2(0, -65));
                    this.node.scale = 0.75;
                    this.node.anchorY = 0;
                }
                break;
            case 'up':
                {
                    const comp = this.node.getComponent(CustomButton);
                    if (comp) {
                        this.node.removeComponent(comp);
                    }
                    if (isMove) {
                        // 上架时物品下落动画
                        this.node.setPosition(cc.v2(0, -15));
                        this.node.stopAllActions();
                        cc.tween(this.node).to(0.1, { y: -55 }).start();
                    } else {
                        this.node.setPosition(cc.v2(0, -55));
                    }
                    this.node.scale = 0.75;
                    this.node.anchorY = 0;
                }
                break;
            case 'down':
                {
                    this.node.setPosition(cc.v2(0, -65));
                }
                break;
            case 'game':
                {
                    this.node.scale = 0;
                    this.node.stopAllActions();
                    cc.tween(this.node).to(0.2, { scale: 0.8 }).start();
                }
                break;
        }
        this.node.active = true;
        // 数量
        this.labNum.string = `${data.num}`;
        // 头像 (货架下和进货时用斜着，其他时候用正的)
        const id = type === 'down' || type === 'game' ? data.id : data.id + "z";
        LoadUtil.instance.setSpriteFrame(this.icon, EResType.Item, id);
    }

    public getRewardData() {
        return this.data;
    }

    private onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case 'itemdown':
                {
                    this.callback && this.callback();
                    this.callback = null;
                }
                break;
        }
    }
}